{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "chat = ChatOpenAI(model=\"gpt-3.5-turbo-1106\", temperature=0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_community.llms import Ollama\n",
    "\n",
    "llm = Ollama(model='llama3', verbose=True)\n",
    "\n",
    "print(llm)\n",
    "\n",
    "llm.invoke(\"hello\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.messages import HumanMessage\n",
    "\n",
    "res = llm.invoke([\n",
    "    HumanMessage(content=\"北京大学在哪里？用中文回答\")\n",
    "])\n",
    "\n",
    "print(f\"{res}\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "res = llm.invoke([HumanMessage(content=\"你刚才说的是什么？用中文回答\")])\n",
    "\n",
    "print(f\"{res}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.messages import AIMessage\n",
    "res = llm.invoke([\n",
    "    HumanMessage(content=\"北京大学在哪里？用中文回答\"),\n",
    "    AIMessage(content=\"北京大学（Peking University）位于中国北京市海淀区。具体地址是：北京市海淀区延世未线1号院号2号楼。\"),\n",
    "    HumanMessage(content=\"你刚才说的是什么？用中文回答\"),\n",
    "    ])\n",
    "\n",
    "print(f\"{res}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Prompt templates"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder\n",
    "\n",
    "prompt = ChatPromptTemplate.from_messages([\n",
    "    (\"system\", \"你是一个中文助手，用你的能力，回答所有的问题\",),\n",
    "    MessagesPlaceholder(variable_name=\"messages\")\n",
    "])\n",
    "\n",
    "llm_chain = prompt | llm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "llm_chain.invoke({\n",
    "    \"messages\": [\n",
    "    HumanMessage(content=\"北京大学在哪里？用中文回答\"),\n",
    "    AIMessage(content=\"北京大学（Peking University）位于中国北京市海淀区。具体地址是：北京市海淀区延世未线1号院号2号楼。\"),\n",
    "    HumanMessage(content=\"你刚才说的是什么？用中文回答\"),\n",
    "    ]\n",
    "})"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Chat History"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[HumanMessage(content='北京大学在哪里？用中文回答'),\n",
       " AIMessage(content='北京大学（Peking University）位于中国北京市海淀区。具体地址是：北京市海淀区延世未线1号院号2号楼。')]"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from langchain.memory import ChatMessageHistory\n",
    "\n",
    "demo_ephemeral_chat_history = ChatMessageHistory()\n",
    "\n",
    "demo_ephemeral_chat_history.add_user_message(\n",
    "    \"北京大学在哪里？用中文回答\"\n",
    ")\n",
    "\n",
    "demo_ephemeral_chat_history.add_ai_message(\n",
    "    \"北京大学（Peking University）位于中国北京市海淀区。具体地址是：北京市海淀区延世未线1号院号2号楼。\"\n",
    ")\n",
    "\n",
    "demo_ephemeral_chat_history.messages"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Automatic history management"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
